/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ez2find.backing;

import com.ez2find.manager.PostManagerLocal;
import com.ez2find.model.Community;
import com.ez2find.model.Hous;
import com.ez2find.model.Image;
import com.ez2find.model.Personal;
import com.ez2find.model.Post;
import com.ez2find.model.Sale;
import com.ez2find.services.EmailLocal;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 * @author org.@le
 */

public class FlagedPostsBean {
 
    @EJB
    private PostManagerLocal ejbPostManager;
    @EJB
    private EmailLocal ejbEmail;
    private Post post;
    private int pagePostings;
    private String searchType;
    private String flagType;

    /** Creates a new instance of FlagedPostsBean */
    public FlagedPostsBean() {
    }

    
    public String delete() {
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
        HttpSession session = request.getSession();
        if (session.isNew()) {
            try {
                String errorPageURL = externalContext.getRequestContextPath() + "/faces/welcome.jsp";
                externalContext.redirect(errorPageURL);
                FacesMessage msg = new FacesMessage("The session has bean expired");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            } catch (IOException ioe) {
                ioe.printStackTrace();
                return null;
            }
        } else {
            try {
                if (post.getTypeReplyTo().equals("masked email")) {
                    ejbEmail.deleteAnonymousEmail(post.getType(), post.getCode(), post.getEmail());
                }
                if (post.getType().equals("house")) {
                    for (Image img : ((Hous) post).getImages()) {
                        File f = new File("D:/Nuevo/ez2find/dist/gfdeploy/site_war/images/" + img.getImage());
                        f.delete();
                    }
                }
                if (post.getType().equals("sale")) {
                    for (Image img : ((Sale) post).getImages()) {
                        File f = new File("D:/Nuevo/ez2find/dist/gfdeploy/site_war/images/" + img.getImage());
                        f.delete();
                    }
                }
                if (post.getType().equals("community")) {
                    for (Image img : ((Community) post).getImages()) {
                        File f = new File("D:/Nuevo/ez2find/dist/gfdeploy/site_war/images/" + img.getImage());
                        f.delete();
                    }
                }
                if (post.getType().equals("personal")) {
                    for (Image img : ((Personal) post).getImages()) {
                        File f = new File("D:/Nuevo/ez2find/dist/gfdeploy/site_war/images/" + img.getImage());
                        f.delete();
                    }
                }
                ejbPostManager.delete(post);
                FacesMessage msg = new FacesMessage("Successful delete your Post");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            } catch (Exception ex) {
                ex.printStackTrace();
                FacesMessage msg = new FacesMessage("Error your Post can't delete");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            }
        }
    }
    
    
    public String getSearchType() {
        if (searchType==null){
            searchType="job";
        }
        return searchType;
    }

    public void setSearchType(String searchType) {
        this.searchType = searchType;
    }

    public String getFlagType() {
        if (flagType==null){
            flagType="miscategorized";
        }
        return flagType;
    }

    public void setFlagType(String flagType) {
        this.flagType = flagType;
    }
    
    public List<Post> getPostsFound(){
        return ejbPostManager.getPostsFound(getSearchType(), getFlagType());
    }

    public int getPagePostings() {
        return pagePostings;
    }

    public void setPagePostings(int pagePostings) {
        this.pagePostings = pagePostings;
    }
}
